﻿using System;
using System.Collections.Generic;

namespace Leaf.NetCore.Log4
{
    /// <summary>
    /// 日志
    /// </summary>
    public class Log4Helper
    {
        static Log4Helper()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "/log4net.xml";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(path));
        }

        public static void Write(string message,
            Exception exception = null,
            LogLevel eval = LogLevel.Info)
        {
            log4net.ILog logs = log4net.LogManager.GetLogger("ReflectionLayout");
            Save(logs, message, exception, eval);
        }

        public static void Write<T>(string message,
            Exception exception = null,
            LogLevel eval = LogLevel.Info)
        {
            log4net.ILog logs = log4net.LogManager.GetLogger("ReflectionLayout");
            Save(logs, message, exception, eval);
        }

        private static void Save(log4net.ILog logs,
            string message,
            Exception exception = null,
            LogLevel eval = LogLevel.Info)
        {
            if (exception != null)
            {
                eval = LogLevel.Fatal;
            }

            switch (eval)
            {
                case LogLevel.Debug:
                    logs.Debug(message);

                    break;

                case LogLevel.Error:
                    logs.Error(message);
                    break;

                case LogLevel.Fatal:
                    logs.Fatal(message, exception);
                    break;

                case LogLevel.Info:
                    logs.Info(message);
                    break;

                case LogLevel.Warning:
                    logs.Warn(message);
                    break;
            }
        }
    }
}